package itsm.isperp.report.paser;

import itsm.isperp.framework.utils.DateUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.joda.time.LocalDate;

/**
 * 
 * 
 * 
 * @author lizx
 * @date 2015-12-6
 * @since 1.0
 * 
 */
public class QueryLineByDayPaser {

	public static QueryLineLegend paser(Date startDate, Date endDate,
			boolean line) {
		QueryLineLegend m = new QueryLineLegend();
		Calendar scal = Calendar.getInstance();
		scal.setTime(startDate);

		LocalDate ld = new LocalDate(endDate);

		String ed = DateUtils.formatDate(endDate);

		List<String> ds = new LinkedList<>();
		ds.add(DateUtils.formatDate(endDate));

		for (int i = 10; i > 1; i--) {

			int l = ld.getDayOfYear() - 1;

			if (l == 0) {
				ld = ld.withYear(ld.getYear() - 1).dayOfYear()
						.withMaximumValue();
			} else {
				ld = ld.withDayOfYear(l);
			}

			ds.add(DateUtils.formatDate(ld.toDate()));
		}

		List<String> ds2 = new LinkedList<>();

		int s = ds.size() - 1;

		for (int i = s; i >= 0; i--) {
			ds2.add(ds.get(i));
		}

		m.setDateFormat("yyyy-mm-dd");
		String sd = DateUtils.formatDate(startDate);

		if (line) {

			m.setStart(sd);
			m.setEnd(ed);
		} else {
			m.setStart(ed);
			m.setEnd(ed);
		}
		m.setValues(ds2);
		return m;
	}
}
